<template>
    <view :style="numBoxStyle" class="mm-numbox-view">
        <view class="mm-numbox">
            <view class="mm-numbox__minus" :class="{'mm-numbox--disabled': disableSubtract||disabled}"
                  @click="_calcValue('subtract')">
                <view>-</view>
            </view>
            <input placeholder-class="mm-numbox__placeholder" class="mm-numbox__value" type="number"
                   :disabled="disabled" :value="inputValue" @blur="_onBlur">
            <view class="mm-numbox__plus" :class="{'mm-numbox--disabled': disableAdd||disabled}"
                  @click="_calcValue('add')">
                <view>+</view>
            </view>
        </view>
    </view>
</template>
<script>
    import { mapState } from 'vuex';

    export default {
        name: 'mm-number-box',
        props: {
            value: {
                type: Number,
                default: 1
            },
            min: {
                type: Number,
                default: 0
            },
            max: {
                type: Number,
                default: 100
            },
            step: {
                type: Number,
                default: 1
            },
            disabled: {
                type: Boolean,
                default: false
            },
            size: {
                type: [Number, String],
                default: 12
            }
        },
        data () {
            return {
                inputValue: this.value
            }
        },
        computed: {
            ...mapState({}),
            disableSubtract () {
                return this.inputValue <= this.min
            },
            disableAdd () {
                return this.inputValue >= this.max
            },
            numBoxStyle () {
                const size = this.size;
                let fontSize = this.$util.getNumber(size, 12);
                return this.$util.object2style({fontSize: `${fontSize}px`})
            }
        },
        watch: {
            value (val) {
                this.inputValue = val;
            },
            inputValue (val) {
                this.$emit('change', val);
            }
        },
        methods: {
            _calcValue (type) {
                if (this.disabled) {
                    return
                }
                const scale = this._getDecimalScale()
                let value = this.inputValue * scale
                let step = this.step * scale
                if (type === 'subtract') {
                    value -= step
                } else if (type === 'add') {
                    value += step
                }
                if (value < this.min || value > this.max) {
                    return
                }
                this.inputValue = value / scale;
            },
            _getDecimalScale () {
                let scale = 1
                // 浮点型
                if (~~this.step !== this.step) {
                    scale = Math.pow(10, (this.step + '').split('.')[1].length)
                }
                return scale
            },
            _onBlur (event) {
                let value = event.detail.value
                if (!value) {
                    this.inputValue = 0
                    return
                }
                value = +value;
                if (value > this.max) {
                    value = this.max
                } else if (value < this.min) {
                    value = this.min
                }
                this.inputValue = value
            }
        }
    }
</script>
<style lang="scss">
    $numbox-btn-width: 2em;
    $numbox-input-width: 3.3em;
    $numbox-height: 2em;
    $uni-font-size-xxl: 1.5em;

    .mm-numbox-view {
        font-size: 12*2upx;
    }

    .mm-numbox {
        display: inline-flex;
        flex-direction: row;
        justify-content: flex-start;
        height: $numbox-height;
        position: relative;

        &__border {
            &:after {
                content: '';
                position: absolute;
                transform-origin: center;
                box-sizing: border-box;
                pointer-events: none;
                top: -50%;
                left: -50%;
                right: -50%;
                bottom: -50%;
                border: 1px solid $uni-border-color;
                border-radius: $uni-border-radius-lg;
                transform: scale(.5);
            }

            .mm-numbox__value {
                border-radius: 0;

                &:after {
                    content: '';
                    position: absolute;
                    transform-origin: center;
                    box-sizing: border-box;
                    pointer-events: none;
                    top: -50%;
                    left: -50%;
                    right: -50%;
                    bottom: -50%;
                    border-style: solid;
                    border-color: $uni-border-color;
                    border-left-width: 1px;
                    border-right-width: 1px;
                    border-top-width: 0;
                    border-bottom-width: 0;
                    transform: scale(.5);
                }
            }
        }

        &__minus,
        &__plus {
            position: relative;
            overflow: hidden;
            margin: 0;
            background-color: #fff;
            width: $numbox-btn-width;
            height: 100%;
            font-size: $uni-font-size-xxl;

            > view {
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                font-size: 1em;
                width: 1.2em;
                height: 1.2em;
                line-height: 1.2em;
                color: #D8D8D8;
                text-align: center;
            }
        }

        &__value {
            position: relative;
            background-color: rgb(245, 248, 247);
            width: $numbox-input-width;
            text-align: center;
            border-radius: 5*2upx;
            font-size: 1em;
            color: #333;
            min-height: 0;
            height: $numbox-height;
            line-height: $numbox-height;
            display: flex;
            align-items: center;
        }

        &__placeholder {
            color: #ccc;
            font-size: 1em;
            height: 1.2em;
            line-height: 1.2em;
        }

        &--disabled {
            color: $uni-text-color-disable;
        }
    }
</style>
